package clientGUI.generalPanels;

import javax.swing.JPanel;
import java.awt.GridBagLayout;
import javax.swing.JLabel;
import java.awt.GridBagConstraints;
import java.awt.Insets;

import javax.swing.JFrame;
import javax.swing.JTextField;
import javax.swing.JButton;

import college.general;


import clientGUI.CollegeClientGUI;

import clientGUI.generalPanels.Controllers.InitController;
import clientGUI.userPanels.UPanel;
import clientserver.Client;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.io.IOException;
import java.awt.Color;

public class ClientInit extends UPanel {
	private JTextField hostField;
	private JTextField portField;
	 

   private   InitController  controller;

	


public Client getClient()
{
	return  controller.getClient();
}



	/**
	 * Create the panel.
	 */
	public ClientInit() {
		super();
		
		
		GridBagLayout gridBagLayout = new GridBagLayout();
		gridBagLayout.columnWidths = new int[]{0, 0, 0, 0, 0};
		gridBagLayout.rowHeights = new int[]{0, 0, 0, 0, 0, 0, 0};
		gridBagLayout.columnWeights = new double[]{0.0, 0.0, 0.0, 1.0, Double.MIN_VALUE};
		gridBagLayout.rowWeights = new double[]{0.0, 0.0, 0.0, 0.0, 0.0, 0.0, Double.MIN_VALUE};
		setLayout(gridBagLayout);
		
		JLabel lblServerIp = new JLabel("Server IP:");
		GridBagConstraints gbc_lblServerIp = new GridBagConstraints();
		gbc_lblServerIp.anchor = GridBagConstraints.EAST;
		gbc_lblServerIp.insets = new Insets(0, 0, 5, 5);
		gbc_lblServerIp.gridx = 2;
		gbc_lblServerIp.gridy = 1;
		add(lblServerIp, gbc_lblServerIp);
		
		hostField = new JTextField();
		hostField.setText("localhost");
		GridBagConstraints gbc_hostField = new GridBagConstraints();
		gbc_hostField.anchor = GridBagConstraints.WEST;
		gbc_hostField.insets = new Insets(0, 0, 5, 0);
		gbc_hostField.gridx = 3;
		gbc_hostField.gridy = 1;
		add(hostField, gbc_hostField);
		hostField.setColumns(10);
		
		JLabel lblConnectionPort = new JLabel("Connection Port:");
		GridBagConstraints gbc_lblConnectionPort = new GridBagConstraints();
		gbc_lblConnectionPort.anchor = GridBagConstraints.EAST;
		gbc_lblConnectionPort.insets = new Insets(0, 0, 5, 5);
		gbc_lblConnectionPort.gridx = 2;
		gbc_lblConnectionPort.gridy = 2;
		add(lblConnectionPort, gbc_lblConnectionPort);
		
		portField = new JTextField();
		portField.setText("1234");
		GridBagConstraints gbc_portField = new GridBagConstraints();
		gbc_portField.insets = new Insets(0, 0, 5, 0);
		gbc_portField.anchor = GridBagConstraints.WEST;
		gbc_portField.gridx = 3;
		gbc_portField.gridy = 2;
		add(portField, gbc_portField);
		portField.setColumns(10);
		
		JButton btnNewButton = new JButton("Connect");
		btnNewButton.addActionListener(new ActionListener() {
			
// Connect Button Listener			
			public void actionPerformed(ActionEvent arg0) {
				
		
				String host;
				int port;
				String p;
				host=hostField.getText();
				p=portField.getText();
				port=Integer.parseInt(p);
				 controller =new InitController(host,port);
			boolean res= controller.isconnected();
	//	
		     	if(res) 	myframe.setLoginPanel();
		     	else
		     		general.alert("Connection Error.You must run server,   And give right ip address and port number");
		     			}
		});
		GridBagConstraints gbc_btnNewButton = new GridBagConstraints();
		gbc_btnNewButton.insets = new Insets(0, 0, 5, 0);
		gbc_btnNewButton.anchor = GridBagConstraints.WEST;
		gbc_btnNewButton.gridx = 3;
		gbc_btnNewButton.gridy = 3;
		add(btnNewButton, gbc_btnNewButton);




	}





}
